package org.example;

import org.testng.TestNG;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Listeners;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Listeners({org.example.ReportListener.class})
public class JenkinsStartInit {

	@Parameters({"jsonParameters"})
	@Test
	public void initExecuteParameters(String jsonParameters) throws IOException {
//		String jsonParameters = "{\"env\":\"prod\",\"cases\":[\"cashier_clientapi.CashierClientLoginTest\",\"cashier_clientapi.RefundTest\"]}";
		//String jsonParameters="{cases=[cashier_clientapi.CashierClientLoginTest], env=prod}";
//		String jsonParameters="{isDingTalk=0,env=prod,cases=[org.testcaseapi.Test1]}";
		System.out.println("打印jenkins参数："+jsonParameters);
		String caseStr = jsonParameters.substring(jsonParameters.indexOf("cases=[")+7,jsonParameters.indexOf("]"));
		System.out.println("打印caseStr:"+caseStr);
		String isDingTalk = jsonParameters.substring(jsonParameters.indexOf("isDingTalk=")+11,jsonParameters.indexOf("isDingTalk=")+12);
		jsonParameters = jsonParameters.replace("isDingTalk=","");
		jsonParameters = jsonParameters.replace(caseStr,"");
		jsonParameters = jsonParameters.replace("cases=[","");
		jsonParameters = jsonParameters.replace(",","");
		jsonParameters = jsonParameters.replace("]","");
		jsonParameters = jsonParameters.replace("{","");
		jsonParameters = jsonParameters.replace("}","");
		jsonParameters = jsonParameters.replace("1","");
		jsonParameters = jsonParameters.replace("0","");
		String environment = jsonParameters.substring(jsonParameters.indexOf("env")+4);
		String groupName = "";
		if (jsonParameters.contains("groupName")){
			environment = jsonParameters.substring(jsonParameters.indexOf("env")+4,jsonParameters.indexOf("groupName"));
			System.out.println("打印environment:"+environment);
			groupName = jsonParameters.substring(jsonParameters.indexOf("groupName")+10);
			System.out.println("打印groupName:"+groupName);
		}

		//PropertiesUtil.writeProperties("env",environment,"cashier.properties");
		//PropertiesUtil.writeProperties("isDingTalk",isDingTalk,"cashier.properties");

		/**
		jsonParameters = jsonParameters.replaceAll(" +","");
		Map map = String2Map.mapStringToMap(jsonParameters);
		//Map map = JSON.parseObject(jsonParameters, HashMap.class);
		System.out.println("标准map："+map.toString());
		String env = map.get("env").toString();
		PropertiesUtil.writeProperties("env",env,"cashier.properties");
		String caseStr = map.get("cases").toString();
		caseStr = caseStr.replace("[","");
		caseStr = caseStr.replace("]","");
		System.out.println("打印caseStr:"+caseStr);
		 **/

		String[] casesArray = caseStr.split(",");
		List casesList = Arrays.asList(casesArray);

		TestNG testNg = new TestNG();
		XmlSuite xmlSuite = new XmlSuite();
		xmlSuite.setName("teslaK8sSuite");

		XmlTest xmlTest = new XmlTest();
		xmlTest.setName(groupName);
		xmlTest.setSuite(xmlSuite);

		xmlSuite.addTest(xmlTest);

		List<XmlClass> classList = new ArrayList<XmlClass>();

		for (int i=0;i<casesList.size();i++){
			String packageName = casesList.get(i).toString();
			classList.add(new XmlClass(packageName));
		}

		xmlTest.setXmlClasses(classList);

		List<XmlSuite> suites = new ArrayList<XmlSuite>();
		suites.add(xmlSuite);
		testNg.setXmlSuites(suites);

		//添加listener
		List<String> listeners = new ArrayList<String>();
		listeners.add("org.example.ReportListener");
		xmlSuite.setListeners(listeners);

		testNg.run();


		/*Set<IReporter> reporters = testNg.getReporters();
		Iterator<IReporter> it = reporters.iterator();
		while(it.hasNext()){
			IReporter reporter = it.next();
			
			System.out.println(reporter.toString());
		}*/


		/**
		 * 组装发送的消息
		 */
		//private String convertToLink() {
		//	JSONObject sendObj = new JSONObject();
		//	JSONObject linkObj = new JSONObject();
		//	linkObj.put("title", projectName);
		//	linkObj.put("text", "用例数：" + totalTestCase + "通过率：" + passRate+ "\n通过：" + passNum + "\t失败：" + failNum + "\t跳过：" + skipNum );
		//	linkObj.put("picUrl", "");
		//	linkObj.put("messageUrl", "http://你的jenkinsIp/view/Selenium/" + "/job/" + "你的job名" + "/allure/");
		//	sendObj.put("msgtype", "link");
		//	sendObj.put("link", linkObj);
		//	return sendObj.toString();
		//}
	}

}
